package com.gensdai.leliang.adapter;

import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.gensdai.leliang.R;
import com.gensdai.leliang.entity.CardCart;
import com.gensdai.leliang.remoteInterface.URLConfig;
import com.gensdai.leliang.view.MyCartcardView;

import java.util.List;

/**
 * Created by Administrator on 2017/5/31.
 */

public class cartcard_list_adapter extends BaseAdapter {

    private List<CardCart> list_data;
    private Context mcontext;
    private Honder honder;
    private Handler mhandler;

    onjiajianClick click;

    public cartcard_list_adapter(Context context, List<CardCart> list, Handler handler, onjiajianClick click) {
        mcontext = context;
        list_data = list;
        mhandler = handler;
        this.click = click;
    }

    @Override
    public int getCount() {
        return list_data.size();
    }

    @Override
    public Object getItem(int position) {
        return list_data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            honder = new Honder();
            convertView = LayoutInflater.from(mcontext).inflate(R.layout.cartcard_list_itme, null);
            honder.cost = (TextView) convertView.findViewById(R.id.cost);
            honder.number = (TextView) convertView.findViewById(R.id.number);
            honder.name = (TextView) convertView.findViewById(R.id.name);
            honder.cartcatdItmeCb = (ImageView) convertView.findViewById(R.id.cartcatd_itme_cb);
            honder.Indent_img = (SimpleDraweeView) convertView.findViewById(R.id.Indent_img);
            honder.jia = (TextView) convertView.findViewById(R.id.jia);
            honder.shuliang = (TextView) convertView.findViewById(R.id.shuliang);
            honder.jian = (TextView) convertView.findViewById(R.id.jian);
            honder.shuxing = (TextView) convertView.findViewById(R.id.shuxing);
            honder.jj = (RelativeLayout) convertView.findViewById(R.id.jj);
            honder.CustomAddOrSubtractView = (MyCartcardView) convertView.findViewById(R.id.jiajian);

            honder.CustomAddOrSubtractView.setCustomAddOrSubtraceCallBack(new MyCartcardView.CustomAddOrSubtraceCallBack() {
                @Override
                public void add(int number) {
                    if (click != null) {
                        click.jia(position, list_data.get(position));
                    }
                }

                @Override
                public void subtrace(int number) {
                    if (click != null) {
                        click.jian(position, list_data.get(position));
                    }
                }

                @Override
                public void touchNumber() {

                }
            });
            convertView.setTag(honder);
        } else {
            honder = (Honder) convertView.getTag();
        }
        if (list_data.get(position).getShopCarState().equals("1")) {
            if (list_data.get(position).getSelect_state().equals("1")) {
                honder.cartcatdItmeCb.setBackground(ContextCompat.getDrawable(mcontext, R.mipmap.yuan));
            } else {
                honder.cartcatdItmeCb.setBackground(ContextCompat.getDrawable(mcontext, R.mipmap.xuanyuan));
            }
            honder.jj.setVisibility(View.VISIBLE);
            honder.shuxing.setText(list_data.get(position).getProductAttributeName());

            honder.CustomAddOrSubtractView.setCentreText(list_data.get(position).getNumber());
            honder.cost.setText(mcontext.getString(R.string.rmb) + list_data.get(position).getPrice());
            honder.number.setText(list_data.get(position).getNumber());

            //库存
            if (!list_data.get(position).getSurplusNumber().equals("")) {
                honder.shuliang.setVisibility(View.VISIBLE);
                honder.shuliang.setText(list_data.get(position).getSurplusNumber());
            } else {
                honder.shuliang.setVisibility(View.GONE);
            }
        } else if (list_data.get(position).getShopCarState().equals("2")) {
            honder.cartcatdItmeCb.setBackground(ContextCompat.getDrawable(mcontext, R.mipmap.shixiao));
            honder.jj.setVisibility(View.GONE);
            honder.shuxing.setText("商品已下架");
            honder.shuliang.setVisibility(View.GONE);
        } else if (list_data.get(position).getShopCarState().equals("3")) {
            honder.cartcatdItmeCb.setBackground(ContextCompat.getDrawable(mcontext, R.mipmap.shixiao));
            honder.jj.setVisibility(View.GONE);
            honder.shuxing.setText("宝贝已经卖光了");
            honder.shuliang.setVisibility(View.GONE);
        }

        honder.name.setText(list_data.get(position).getShopname());


        if (list_data.get(position).getIc_path().length() > 5) {
            Uri uri = Uri.parse(URLConfig.BASEDOMAIN + list_data.get(position).getIc_path());
            honder.Indent_img.setImageURI(uri);
        }
        honder.Indent_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (click != null) {
                    click.tu(position);
                }
            }
        });
        return convertView;
    }

    private String zhuanhuan(String mongey, String number) {
        String gold = Double.parseDouble(mongey) * Double.parseDouble(number) + "";
        return gold;
    }

    /**
     * 回调
     *
     * @param position
     * @param flag
     */
    private void sendmsg(final int position, int flag) {
        Message msg = new Message();
        msg.what = flag;
        msg.obj = position;
        mhandler.sendMessage(msg);
    }

    public interface onjiajianClick {
        void jia(int position, CardCart cardCart);

        void jian(int position, CardCart cardCart);

        void tu(int position);
    }

    class Honder {
        ImageView cartcatdItmeCb;
        SimpleDraweeView Indent_img;// 产品图片
        TextView cost;
        TextView shuxing; // 商品属性
        TextView name;
        TextView number;
        TextView shuliang; //剩余数量
        TextView jia;
        TextView jian;
        MyCartcardView CustomAddOrSubtractView;
        RelativeLayout jj; // 底层选择增减按钮父布局
    }
}
